package com.duqc.response;

import lombok.Data;

import java.io.Serializable;

/**
 * @ClassName ResultMsg
 * @Description
 * @Author duqc
 * @Date 2021/9/7
 **/
@Data
public class ResultMsg<T> implements Serializable {

    private int resultCode;

    private String resultMsg;

    private T resultData;

    private ResultEnum resultEnum;

    private ResultMsg() {
    }

    private ResultMsg(int resultCode, String resultMsg, T resultData) {
        this.resultCode = resultCode;
        this.resultMsg = resultMsg;
        this.resultData = resultData;
    }

    private ResultMsg(T resultData) {
        this.resultData = resultData;
        this.resultEnum = ResultEnum.SUCCESS;
    }

    private ResultMsg(T resultData, ResultEnum resultEnum) {
        this.resultData = resultData;
        this.resultEnum = resultEnum;
    }

    private ResultMsg(ResultEnum resultEnum) {
        this.resultEnum = resultEnum;
    }

    /**
     * 返回成功，主要处理返回信息为void类型
     *
     * @return ResultMsg
     */
    public static <T> ResultMsg<T> success() {
        ResultMsg<T> result = new ResultMsg<>();
        result.setResultCode(ResultEnum.SUCCESS.getCode());
        result.setResultMsg(ResultEnum.SUCCESS.getMessage());
        result.setResultEnum(ResultEnum.SUCCESS);
        return result;
    }

    /**
     * 返回成功
     *
     * @param data 成功的结果信息
     * @return ResultMsg
     */
    public static <T> ResultMsg<T> success(T data) {
        ResultMsg<T> result = new ResultMsg<>();
        result.setResultMsg(ResultEnum.SUCCESS.getMessage());
        result.setResultCode(ResultEnum.SUCCESS.getCode());
        result.setResultEnum(ResultEnum.SUCCESS);
        result.setResultData(data);
        return result;
    }

    /**
     * 返回成功
     *
     * @param data       成功的结果信息
     * @param resultEnum 成功枚举
     * @return ResultMsg
     */
    public static <T> ResultMsg<T> success(T data, ResultEnum resultEnum) {
        ResultMsg<T> result = new ResultMsg<>();
        result.setResultMsg(resultEnum.getMessage());
        result.setResultCode(resultEnum.getCode());
        result.setResultEnum(resultEnum);
        result.setResultData(data);
        return result;
    }

    /**
     * 错误输出
     *
     * @param resultEnum 错误枚举
     * @return ResultMsg
     */
    public static <T> ResultMsg<T> failure(ResultEnum resultEnum) {
        ResultMsg<T> result = new ResultMsg<>();
        result.setResultEnum(resultEnum);
        result.setResultCode(resultEnum.getCode());
        result.setResultMsg(resultEnum.getMessage());
        return result;
    }

    /**
     * 错误输出
     *
     * @param resultEnum 错误枚举
     * @param data       错误数据
     * @return ResultMsg
     */
    public static <T> ResultMsg<T> failure(ResultEnum resultEnum, T data) {
        ResultMsg<T> result = new ResultMsg<>();
        result.setResultEnum(resultEnum);
        result.setResultCode(resultEnum.getCode());
        result.setResultMsg(resultEnum.getMessage());
        result.setResultData(data);
        return result;
    }

    /**
     * 错误输出
     *
     * @param resultEnum 错误枚举
     * @param resultMsg  错误信息
     * @return ResultMsg
     */
    public static <T> ResultMsg<T> failure(ResultEnum resultEnum, String resultMsg) {
        ResultMsg<T> result = new ResultMsg<>();
        result.setResultCode(resultEnum.getCode());
        result.setResultMsg(resultMsg);
        return result;
    }

    /**
     * 错误输出 默认错误输出
     *
     * @param message 错误信息
     * @return ResultMsg
     */
    public static <T> ResultMsg<T> failure(String message) {
        ResultMsg<T> result = new ResultMsg<>();
        result.setResultCode(ResultEnum.FAILED.getCode());
        result.setResultMsg(message);
        return result;
    }

    /**
     * 错误输出
     *
     * @param code 错误编码
     * @param msg  错误信息
     * @return ResultMsg
     */
    public static <T> ResultMsg<T> failure(int code, String msg) {
        ResultMsg<T> result = new ResultMsg<>();
        result.setResultCode(code);
        result.setResultMsg(msg);
        return result;
    }

}
